package com.winit.openapi.apiadapter.controller;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSONObject;
import com.winit.openapi.apiadapter.controller.util.ApiClient;
import com.winit.openapi.constants.ApiConstant;
import com.winit.openapi.constants.ErrorCode;
import com.winit.openapi.controller.BaseController;
import com.winit.openapi.exception.ApiAdapteException;
import com.winit.openapi.exception.ApiException;
import com.winit.openapi.model.RequestMsg;
import com.winit.openapi.model.ResponseMsg;

/**
 * API适配父类
 * 
 * @version <pre>
 * Author	Version		Date		Changes
 * minghao.deng 	1.0  		2016年12月23日 	Created
 *
 * </pre>
 * @since 1.
 */
public abstract class AbstractAdapteController extends BaseController {

    private static final Logger logger              = LoggerFactory.getLogger(AbstractAdapteController.class);

    /**
     * 真正实现适配流程的方法
     * 
     * @param requestMsg
     * @param responseMsg
     */
    public String doAdapte(RequestMsg requestMsg, ResponseMsg responseMsg) {
        // 1.校验入参
        validateInputJson(requestMsg);
        
        doAdapteInOpenAPI(requestMsg, responseMsg);
//        // 2.判断是否迁仓
//        if (checkCustomerTansferConfig(requestMsg, responseMsg)) {
//            // 3.具体适配方案：新仓在openAPI处理
//            doAdapteInOpenAPI(requestMsg, responseMsg);
//        } else {
//            // 3.具体适配方案：老仓转发至3E处理
//            postRequestTo3E(requestMsg, responseMsg);
//        }
        
        return API_ADAPTED_SUCCESS;

    }

    /**
     * 发送Post请求到3E，针对没有做适配的接口
     * 
     * @param requestMsg
     * @param responseMsg
     */
    public void postRequestTo3E(RequestMsg requestMsg, ResponseMsg responseMsg) {
        ApiClient client = new ApiClient();
        String result = client.postTo3E(requestMsg, config.getErpDomain() + ApiConstant.AD_3E_API_SUFFIX);
        if (StringUtils.isNotEmpty(result)) {
            JSONObject itemJson = JSONObject.parseObject(result);
            responseMsg.setCode(itemJson.getString("code"));
            responseMsg.setMsg(itemJson.getString("msg"));
            responseMsg.setData(itemJson.get("data"));
            if (!ErrorCode.SUCCESS.equals(responseMsg.getCode())) {
                throw new ApiAdapteException(responseMsg.getCode(), responseMsg.getMsg());
            }
        } else {
            logger.error("调用action:" + requestMsg.getAction() + "返回值为空");
            throw new ApiException(ErrorCode.SYSTEM_ERROR);
        }
    }

   
    /**
     * 判断是否做了出库迁移
     * 
     * @return
     */
    protected abstract boolean checkCustomerTansferConfig(RequestMsg requestMsg, ResponseMsg responseMsg);

    /**
     * 对做了迁仓的在openAPI里面做适配
     */
    protected abstract void doAdapteInOpenAPI(RequestMsg requestMsg, ResponseMsg responseMsg);
    
    /**
     * 参数校验
     * @param requestMsg
     */
    protected abstract void validateInputJson(RequestMsg requestMsg);

}
